import 'package:flutter/material.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:form_builder_validators/form_builder_validators.dart';
import 'package:get/get.dart';
import 'package:hj_crm/http/request_repository.dart';
import 'package:hj_crm/utils/form_utils.dart';

/// 出库表单
class DistributorOutboundForm extends StatefulWidget {
  final String uId;

  const DistributorOutboundForm({super.key, required this.uId});

  @override
  State<DistributorOutboundForm> createState() => _DistributorOutboundFormState();
}

class _DistributorOutboundFormState extends State<DistributorOutboundForm> {
  RequestRepository repository = Get.find<RequestRepository>();

  final _formKey = GlobalKey<FormBuilderState>();

  late TextEditingController _productNameController;
  late TextEditingController _productNumController;
  late TextEditingController _outboundTimeController;

  late FocusNode _productNameFocusNode;
  late FocusNode _productNumFocusNode;
  late FocusNode _outboundTimeFocusNode;

  @override
  void initState() {
    super.initState();
    _productNameController = TextEditingController();
    _productNumController = TextEditingController();
    _outboundTimeController = TextEditingController();

    _productNameFocusNode = FocusNode();
    _productNumFocusNode = FocusNode();
    _outboundTimeFocusNode = FocusNode();
  }

  @override
  void dispose() {
    _productNameController.dispose();
    _productNumController.dispose();
    _outboundTimeController.dispose();

    _productNameFocusNode.dispose();
    _productNumFocusNode.dispose();
    _outboundTimeFocusNode.dispose();

    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () => FocusScope.of(context).unfocus(),
      child: Scaffold(
        appBar: AppBar(
          title: const Text('出库记录'),
        ),
        body: SingleChildScrollView(
          child: FormBuilder(
            key: _formKey,
            child: Container(
              padding: const EdgeInsets.symmetric(horizontal: 20),
              child: Column(
                children: [
                  FormBuilderTextField(
                    name: 'productName',
                    decoration: const InputDecoration(labelText: '商品名称'),
                    validator: FormBuilderValidators.compose([
                      FormBuilderValidators.required(errorText: '请输入商品名称'),
                    ]),
                    keyboardType: TextInputType.text,
                    controller: _productNameController,
                    focusNode: _productNameFocusNode,
                    textInputAction: TextInputAction.next,
                    onEditingComplete: () {
                      FocusScope.of(context).requestFocus(_productNumFocusNode);
                    },
                  ),
                  FormBuilderTextField(
                    name: 'productNum',
                    decoration: const InputDecoration(labelText: '出库数量'),
                    validator: FormBuilderValidators.compose([
                      FormBuilderValidators.required(errorText: '请输入出库数量'),
                      FormBuilderValidators.numeric(errorText: '请输入数字'),
                    ]),
                    keyboardType: TextInputType.number,
                    textInputAction: TextInputAction.next,
                    controller: _productNumController,
                    focusNode: _productNumFocusNode,
                    onEditingComplete: () {
                      FocusScope.of(context).requestFocus(_outboundTimeFocusNode);
                    },
                  ),
                  FormBuilderTextField(
                    name: 'outboundTime',
                    readOnly: true,
                    decoration: const InputDecoration(labelText: '出库时间'),
                    validator: FormBuilderValidators.compose([
                      FormBuilderValidators.required(errorText: '请选择出库时间'),
                    ]),
                    controller: _outboundTimeController,
                    focusNode: _outboundTimeFocusNode,
                    onTap: () {
                      openDatePicker(context, DateItemKind.orderDate, (value) {
                        _outboundTimeController.text = value;
                      });
                    },
                  ),
                  SizedBox(height: 20),
                  ElevatedButton(
                    onPressed: () {
                      //{"userInfoId":"1877553049627545600","productName":"123","productNum":"2","amount":"99","outbound":"2025-1-7 0:0:0"}
                      /// 验证表单
                      if (!_formKey.currentState!.saveAndValidate()) {
                        return;
                      }
                      Map<String, dynamic> map = {
                        "customerInfoId": widget.uId,
                        "productName": _productNameController.text,
                        "productNum": _productNumController.text,
                        "buyTime": _outboundTimeController.text + ' 00:00:00',
                      };
                      repository.addDistributorOutbound(
                          params: map,
                          success: (data) {
                            Navigator.pop(context, true);
                          },
                          fail: (code, msg) {});
                    },
                    child: const Text('保存'),
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}
